﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.XPath;
using System.Drawing;

using GaugeEngine.Graphics;

namespace GaugeEngine
{
    public delegate Behaviour BehaviourCreator(Game g, Actor a, XPathNavigator xnav);

    public static class ContentFactory
    {
        private static Dictionary<string, BehaviourCreator> m_behaviourCreation = new Dictionary<string,BehaviourCreator>();

        public static void RegisterBehaviour(string name, BehaviourCreator creator)
        {
            m_behaviourCreation.Add(name, creator);
        }

        #region loadSprite

        public static Sprite LoadSprite(string filename)
        {
            Sprite s = new Sprite((Bitmap)Image.FromFile(filename));
            return s;
        }

        public static SpriteList LoadSpriteList(string filename)
        {
            SpriteList output = new SpriteList();
            XPathDocument xdoc = new XPathDocument(filename);
            XPathNavigator xnav = xdoc.CreateNavigator();
            xnav.MoveToFirstChild();
            xnav.MoveToFirstChild();
            do
            {
                Sprite newsprite = new Sprite((Bitmap)Image.FromFile(xnav.GetAttribute("imagelocation", "")));
                string n = xnav.GetAttribute("name", "");
                output.Add(n, newsprite);
            } while (xnav.MoveToNext());   
            return output;
        }

        #endregion

        #region loadActor

        public static Actor LoadActor(Game g, string filename)
        {
            XPathDocument xdoc = new XPathDocument(filename);
            XPathNavigator xnav = xdoc.CreateNavigator();
            xnav.MoveToFirstChild();

            string name = xnav.GetAttribute("name", "");

            string vectorpos = xnav.GetAttribute("start_position", "");
            string[] vectorsplit = vectorpos.Split(',');
            Vector start_position = new Vector(Convert.ToSingle(vectorsplit[0]), Convert.ToSingle(vectorsplit[1]));

            Actor a = new Actor(name);
            a.Position = start_position;
            xnav.MoveToFirstChild();

            do
            {
                 a.AddBehaviour(m_behaviourCreation[xnav.Name](g, a, xnav));
            } while (xnav.MoveToNext());  


            return a;
        }
        #endregion

        #region loadLevel

        public static Level LoadLevel(Game g, string filename)
        {
            Level lvl = new Level(g);

            XPathDocument xdoc = new XPathDocument(filename);
            XPathNavigator xnav = xdoc.CreateNavigator();
            xnav.MoveToFirstChild();
            xnav.MoveToChild("data", "");

            xnav.MoveToFirstChild();

            do
            {
                string name = xnav.GetAttribute("name", "");

                string vectorpos = xnav.GetAttribute("start_position", "");
                string[] vectorsplit = vectorpos.Split(',');
                Vector start_position = new Vector(Convert.ToSingle(vectorsplit[0]), Convert.ToSingle(vectorsplit[1]));

                Actor a = new Actor(name);
                a.Position = start_position;

                xnav.MoveToFirstChild();

                do
                {
                    Behaviour behav = m_behaviourCreation[xnav.Name](g, a, xnav);
                    a.AddBehaviour(behav);
                } while (xnav.MoveToNext());  

                lvl.AddActor(a);
                xnav.MoveToParent();
            } while (xnav.MoveToNext());

            return lvl;
        }

        #endregion
    }
}
